Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Declaration (computer programming)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Declaration_(computer_programming)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Declaration_computer_programming rootpage-Declaration_computer_programming skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Declaration (computer programming)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */


.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}


/* end https://en.wikipedia.org/ */
</style>
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>declaration</b> is a <a href="Language_construct" title="Language construct">language construct</a> specifying <a href="Identifier_(computer_programming)" class="mw-redirect" title="Identifier (computer programming)">identifier</a> properties: it declares a word's (identifier's) meaning.<sup id="cite_ref-c11_1-0" class="reference"><a href="#cite_note-c11-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> Declarations are most commonly used for <a href="Subroutine" class="mw-redirect" title="Subroutine">functions</a>, <a href="Variable_(computer_science)" title="Variable (computer science)">variables</a>, <a href="Constant_(computer_programming)" title="Constant (computer programming)">constants</a>, and <a href="Class_(computer_programming)" title="Class (computer programming)">classes</a>, but can also be used for other entities such as <a href="Enumerator_(computer_science)" title="Enumerator (computer science)">enumerations</a> and type definitions.<sup id="cite_ref-c11_1-1" class="reference"><a href="#cite_note-c11-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> Beyond the name (the identifier itself) and the kind of entity (function, variable, etc.), declarations typically specify the <a href="Data_type" title="Data type">data type</a> (for variables and constants), or the <a href="Type_signature" title="Type signature">type signature</a> (for functions); types may also include dimensions, such as for <a href="Array_data_structure" class="mw-redirect" title="Array data structure">arrays</a>. A declaration is used to announce the existence of the entity to the <a href="Compiler" title="Compiler">compiler</a>; this is important in those <a href="Strongly_typed" class="mw-redirect" title="Strongly typed">strongly typed</a> languages that require functions, variables, and constants, and their types to be specified with a declaration before use, and is used in <a href="Forward_declaration" title="Forward declaration">forward declaration</a>.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> The term "declaration" is frequently contrasted with the term "definition",<sup id="cite_ref-c11_1-2" class="reference"><a href="#cite_note-c11-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> but meaning and usage varies significantly between languages; see below.
</p><p>Declarations are particularly prominent in languages in the <a href="ALGOL" title="ALGOL">ALGOL</a> tradition, including the <a href="BCPL" title="BCPL">BCPL</a> family, most prominently <a href="C_(programming_language)" title="C (programming language)">C</a> and <a href="C%2B%2B" title="C++">C++</a>, and also <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>. <a href="Java_(programming_language)" title="Java (programming language)">Java</a> uses the term "declaration", though Java does not require separate declarations and definitions.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Declaration_vs._definition">Declaration vs. definition</h2></div>
<p>One basic dichotomy is whether or not a declaration contains a definition: for example, whether a variable or constant declaration <a href="Initialization_(programming)" title="Initialization (programming)">specifies its value</a>, or only its type; and similarly whether a declaration of a function specifies the body (<a href="Implementation" title="Implementation">implementation</a>) of the function, or only its type signature.<sup id="cite_ref-c11_1-3" class="reference"><a href="#cite_note-c11-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> Not all languages make this distinction: in many languages, declarations always include a definition, and may be referred to as either "declarations" or "definitions", depending on the language.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>a<span class="cite-bracket">]</span></a></sup> However, these concepts are distinguished in languages that require declaration before use (for which forward declarations are used), and in languages where interface and implementation are separated: the interface contains declarations, the implementation contains definitions.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>b<span class="cite-bracket">]</span></a></sup>
</p><p>In informal usage, a "declaration" refers only to a pure declaration (types only, no value or body), while a "definition" refers to a declaration that includes a value or body. However, in formal usage (in language specifications), "declaration" includes <i>both</i> of these senses, with finer distinctions by language: in C and C++, a declaration of a function that does not include a body is called a <a href="Function_prototype" title="Function prototype">function prototype</a>, while a declaration of a function that does include a body is called a "function definition". In Java declarations occur in two forms. For public methods they can be presented in interfaces as method signatures, which consist of the method names, input types and output type. A similar notation can be used in the definition of <a href="Abstract_method" class="mw-redirect" title="Abstract method">abstract methods</a>, which do not contain a definition. The enclosing class can be instantiated, rather a new derived class, which provides the definition of the method, would need to be created in order to create an <a href="Instance_(computer_science)" title="Instance (computer science)">instance</a> of the class. Starting with <a href="Java_8" class="mw-redirect" title="Java 8">Java 8</a>, the lambda expression was included in the language, which could be viewed as a function declaration.
</p>
<div class="mw-heading mw-heading2"><h2 id="Declarations_and_definitions">Declarations and definitions</h2></div>
<p>In the C-family of programming languages, declarations are often collected into <a href="Header_file" class="mw-redirect" title="Header file">header files</a>, which are included in other source files that reference and use these declarations, but don't have access to the definition. The information in the header file provides the interface between code that uses the declaration and that which defines it, a form of <a href="Information_hiding" title="Information hiding">information hiding</a>. A declaration is often used in order to access functions or variables defined in different source files, or in a <a href="Library_(computing)" title="Library (computing)">library</a>. A mismatch between the definition type and the declaration type generates a compiler error.
</p><p>For variables, definitions assign values to an area of memory that was reserved during the declaration phase. For functions, definitions supply the function body. While a variable or function may be declared many times, it is typically defined once (in <a href="C%2B%2B" title="C++">C++</a>, this is known as the <a href="One_Definition_Rule" title="One Definition Rule">One Definition Rule</a> or ODR).
</p><p>Dynamic languages such as <a href="JavaScript" title="JavaScript">JavaScript</a> or <a href="Python_(programming_language)" title="Python (programming language)">Python</a> generally allow functions to be redefined, that is, <a href="Name_binding" title="Name binding">re-bound</a>; a function is a variable much like any other, with a name and a value (the definition).
</p><p>Here are some examples of declarations that are not definitions, in C:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">extern</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">example1</span><span class="p">;</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">example2</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">example3</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
</pre></div>
<p>Here are some examples of declarations that are definitions, again in C:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">char</span><span class="w"> </span><span class="n">example1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Outside of a function definition it will be initialized to zero. */</span>
<span class="kt">int</span><span class="w"> </span><span class="n">example2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">example3</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* definition between braces */</span><span class="w"> </span><span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Undefined_variables">Undefined variables</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Undefined_variable" title="Undefined variable">Undefined variable</a></div>
<p>In some programming languages, an implicit declaration is provided the first time such a variable is encountered at <a href="Compile_time" title="Compile time">compile time</a>. In other languages, such a usage is considered to be an error, which may result in a diagnostic message. Some languages have started out with the implicit declaration behavior, but as they matured they provided an option to disable it (e.g. <a href="Perl" title="Perl">Perl</a>'s "<code>use strict</code>" or <a href="Visual_Basic" title="Visual Basic">Visual Basic</a>'s "<code>Option Explicit</code>").
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Scope_(computer_science)" title="Scope (computer science)">Scope (computer science)</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-lower-alpha">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text">For example, Java uses "declaration" (class declaration, method declaration), while Python uses "definition" (class definition, function definition).<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text">This distinction is observed in Pascal "units" (modules), and in conventional C and C++ code organization, which has <a href="Header_file" class="mw-redirect" title="Header file">header files</a> consisting largely of pure declarations, and <a href="Source_file" class="mw-redirect" title="Source file">source files</a> consisting of definitions, though this is not always strictly observed, nor enforced by the language.</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-c11-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-c11_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-c11_1-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-c11_1-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-c11_1-3"><sup><i><b>d</b></i></sup></a></span> <span class="reference-text">"A declaration specifies the interpretation and attributes of a set of identifiers. A <i>definition</i> of an identifier is a declaration for that identifier that:
<ul><li>for an object [variable or constant], causes storage to be reserved for that object;</li>
<li>for a function, includes the function body;</li>
<li>for an enumeration constant, is the (only) declaration of the identifier;</li>
<li>for a typedef name, is the first (or only) declaration of the identifier."</li></ul>
C11 specification, 6.7: Declarations, paragraph 5.</span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFMike_Banahan" class="citation web cs1">Mike Banahan. <a rel="nofollow" class="external text" href="http://publications.gbdirect.co.uk/c_book/chapter2/variable_declaration.html">"2.5. Declaration of variables"</a>. GBdirect<span class="reference-accessdate">. Retrieved <span class="nowrap">2011-06-08</span></span>. <q>[A] declaration [...] introduces just the name and type of something but allocates no storage[...].</q></cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://docs.python.org/2/reference/compound_stmts.html">7. Compound statements</a>, <i>The Python Language Reference</i></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://www.cprogramming.com/declare_vs_define.html">Declare vs Define in C and C++</a>, Alex Allain</li>
<li><a rel="nofollow" class="external text" href="http://publications.gbdirect.co.uk/c_book/chapter8/declarations_and_definitions.html">8.2. Declarations, Definitions and Accessibility</a>, <i>The C Book,</i> GBdirect</li>
<li><a rel="nofollow" class="external text" href="http://msdn.microsoft.com/en-us/library/0e5kx78b.aspx">Declarations and Definitions (C++)</a>, MSDN
<dl><dd>"Declarations tell the compiler that a program element or name exists. Definitions specify what code or data the name describes."</dd></dl></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2024-08-27" href="https://en.wikipedia.org/wiki/?title=Declaration_(computer_programming)&amp;oldid=1242507682">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>